﻿Chenxin.UI.ItemsManage = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: '/service/logic/ItemsManage.ashx',
    createSearch: function() {
        return new Chenxin.UI.ItemsManageSearch({
            scope: this
        });
    },
    createGrid: function() {
        return new Chenxin.UI.JsonGrid({
            actionUrl: this.actionUrl + '?op=getlist',
            pageSize: 100,
            hasPage: true,
            cellBorder: true,
            height: 400,
            autoLoad: false,
            renderTo: 'ItemsManage_table',
            hasCheckBox: false,
            id: 'searchtable',
            modelId: 'RecordId',
            title: '项目列表',
            scope: this,
            columns: {
                ItemsName: {
                    width: 150,
                    head: '项目名称',
                    sortable: true
                },
                ItemsRegion: {
                    width: 80,
                    head: '项目区域',
                    sortable: true
                },
                ItemsAddress: {
                    width: 250,
                    head: '项目地址',
                    sortable: true
                },
                CityName: {
                    width: 150,
                    head: '所属城市',
                    sortable: true
                },
                CompanyName: {
                    width: 150,
                    head: '所属公司',
                    sortable: true
                }
            }
        });
    },
    initEvent: function() {
        var _this = this;
        $('#additem').click(function() {
            _this.AddItem();
        });
        $('#edititem').click(function() {
            _this.EditItem();
        });
        $('#delitem').click(function() {
            _this.DelItem();
        });
    },
    AddItem: function() {
        if (this.ItemForm == undefined) {
            this.ItemForm = new Chenxin.UI.ItemForm({
                scope: this,
                afterGetFormJson: function(json) {
                    if (this.ItemForm.op == 'add')
                        delete json.RecordId;
                }
            });
        }
        this.ItemForm.op = 'add';
        this.ItemForm.setTitle('新增项目');
        if (this.ItemForm.show()) {
            this.ItemForm.reset();
        }
    },
    EditItem: function() {
        var _this = this;
        if (!_this.currentId) {
            alert('请先选择一条记录！');
            return;
        }
        if (this.ItemForm == undefined) {
            this.ItemForm = new Chenxin.UI.ItemForm({
                scope: this,
                afterGetFormJson: function(json) {
                    if (this.ItemForm.op == 'add')
                        delete json.RecordId;
                }
            });
        }
        this.ItemForm.op = 'edit';
        this.ItemForm.setTitle('编辑项目');
        var curData = {};
        Chenxin.apply(curData, this.grid.getCurrentRecord());
        if (this.ItemForm.show()) {
            this.ItemForm.reset();
            this.ItemForm.fitData(curData);
        }
    },
    DelItem: function() {
        var _this = this;
        if (!_this.currentId) {
            alert('请先选择一条记录！');
            return;
        }
        this.del('del', '是否删除该项目？');
    },
    initComponent: function() {
        Chenxin.UI.ItemsManage.superclass.initComponent.call(this);
    }
});


/**
*查询面板
*/
Chenxin.UI.ItemsManageSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'ItemsManage_search',
    initEvent: function() {
        var _this = this;

        //查询按钮
        $('#btnSreach').click(function() {
            _this.scope.reloadGrid("",0);
        });

        $('#txtKeyword').defText({
            deftxt: '输入项目名称'
        });

    },
    createComboList: function() {
        var _this = this;
        //城市
        var searchCity = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#city',
            name: 'CityId',
            tbxWidth: 80,
            inModelType: 'val',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    searchCompany.clearItems();
                } else {
                    searchCompany.param.fatherid = this.getValue();
                    searchCompany.reload();
                }
            }
        });

        //公司
        var searchCompany = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
            containerId: '#company',
            name: 'CompanyId',
            tbxWidth: 80,
            inModelType: 'val'
        });
        return [searchCity, searchCompany];
    },
    initComponent: function() {
        Chenxin.UI.ItemsManageSearch.superclass.initComponent.call(this);
    }
});

$(document).ready(function() {
    this.ItemsManage = new Chenxin.UI.ItemsManage();
});